///////////////////////////////////////////////////////////////////////////////
//
// Copyright
//
///////////////////////////////////////////////////////////////////////////////

package ru.goodcore.resource
{

import flash.utils.getClassName;

import ru.goodcore.net.URI;
import ru.goodcore.utils.crypto.MD5;

/**
 * 
 * @author					Dimarik
 * @version					1.0
 * @playerversion			Flash 9
 * @langversion				3.0
 *
 * @keyword					resourcedefinition, resourcemanager, resourcebehavior
 */
public class ResourceDefinition
{

	include "../../../includes/core/Version.as";

	//-------------------------------------------------------------------------
	//
	//  Constructor
	//
	//-------------------------------------------------------------------------

	public function ResourceDefinition(uri:URI, behavior:ResourceBehavior = null) {
		super();
		this._uri = uri;

		if ( !behavior ) {
			behavior = ResourceBehavior.DEFAULT;
		}

		this._behavior = behavior;

		this._signature = MD5.hash( this.uri.toDisplayString() );

		// Define QName
		var path:	String = uri.path;

		path = path.replace(/[\\\/]/g, '.');
		path = path.replace(/\|/g, '$');
		path = path.replace(/^\./g, '');

		var index:int = path.lastIndexOf('.');

		var namespc:		String = '';
		var localName:		String;

		if (index >= 0) {
			namespc = path.substr(0, index);
			localName = path.substr(index + 1);
		} else {
			localName = path;
		}

		this._qname = new QName(namespc, localName);		
	}


	//-------------------------------------------------------------------------
	//
	//  Properties
	//
	//-------------------------------------------------------------------------


	//-------------------------------------
	//  uri
	//-------------------------------------
	/**
	 * @private
	 */
	private var _uri:	URI;

	public function get uri():URI {
		return this._uri;
	}

	//-------------------------------------
	//  behavior
	//-------------------------------------
	/**
	 * @private
	 */
	private var _behavior:	ResourceBehavior;

	public function get behavior():ResourceBehavior {
		return this._behavior;
	}

	//---------------------------------
	//  signature
	//---------------------------------
	/**
	 * @private
	 */
	private var _signature:	String;

	public function get signature():String {
		return this._signature;
	}

	//---------------------------------
	//  qname
	//---------------------------------
	/**
	 * @private
	 */
	private var _qname:	QName;

	public function get qname():QName {
		return this._qname;
	}

	//-------------------------------------------------------------------------
	//
	//  Public methods
	//
	//-------------------------------------------------------------------------

	public function toString():String {
		return	'[' + getClassName(this) + 
				': signature="' + this._signature + '"' + 
				'; behavior="' + this._behavior.toString() + '"' +
				'; uri="' + this.uri.toDisplayString() + '"' +
				'; qname="' + this._qname.toString() + '"]';
	}

}
}